<template>
  <!-- 企业经营范围列表  -->
  <div class="app-container">
    <div class="form-container">
      <el-form ref="filters" :inline="true" label-width="80px" :model="filters">
        <el-form-item label="范围名称" prop="businessName">
          <el-input
            v-model="filters.businessName"
            size="small"
            placeholder="请输入名称"
            clearable
          />
        </el-form-item>
      </el-form>
    </div>
    <div class="btns-group">
      <superviseBtns :single="single" has-permi="businessScope" />
    </div>
    <!-- 树形表格展示start -->
    <div ref="table">
      <el-table
        ref="multipleTable"
        v-loading="tableLoading"
        :data="treeData"
        :row-style="rowClass"
        stripe
        class="table-list"
        :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height-tree' : 'el-body-page-roll-height-tree') :
          (needTagsView===true ? 'el-body-page-roll-tag-tree' : 'el-body-page-roll-tree')"
        row-key="id"
        :default-expand-all="false"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        @selection-change="selectCheck"
        @row-click="rowChecked"
      >
        <el-table-column type="selection" width="38" />
        <el-table-column prop="businessName" header-align="center" label="范围名称" min-width="180" :show-overflow-tooltip="true" />
        <el-table-column prop="orderNum" align="center" label="排序" min-width="40" />
        <el-table-column prop="remark" align="center" label="备注" :show-overflow-tooltip="true" min-width="180" />
      </el-table>
    </div>
    <!-- 树形表格展示end-->

    <!-- 设备新增 -->
    <addDialog ref="addDialog" />
    <!-- 设备详情 -->
    <detailDialog ref="detailDialog" />
  </div>
</template>

<script>
/** 页面接口 start*/
import { listBusinessScope, removeBusinessScope } from '@/api/superviseBaseInfo/natureOfBusiness/index'
/** 页面接口 end*/
import { mapState } from 'vuex'
/** 页面组件 start*/
// 新增弹框
import addDialog from './components/addDialog'
// 详情弹框
import detailDialog from './components/detailDialog'
// 按钮组件
import superviseBtns from '@/components/Common/Btns/superviseBtns'
/** 页面组件 end*/
export default {
  components: { detailDialog, addDialog, superviseBtns },
  data() {
    return {
      // 高级设置显示隐藏
      highLevelSet: false,
      // 新增工作量按钮
      single: true,
      // 搜索字段
      filters: {
        // 查询参数
        pageNum: 1,
        pageSize: this.fristPageSize,
        businessName: ''
      },
      // 高亮数组
      selectRow: [],
      rowTableData: [],
      treeData: [],
      total: 0,
      // 选中行的id
      multipleSelection: [],
      // 表格加载是否显示等待图标
      tableLoading: false
    }
  },
  computed: {
    ...mapState({
      needTagsView: state => state.settings.tagsView
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    const _this = this// 赋值vue的this
    window.onresize = () => {
      // 调用methods中的事件
      _this.AddTaleData(_this.treeData, _this.total)
    }
    // 跳转路由时关闭窗口提交
    this.$once('hook:beforeDestroy', () => {
      window.onresize = null
    })
  },
  methods: {
    // 加载树形表格
    async getList() {
      this.tableLoading = true
      try {
        const { data } = await listBusinessScope(this.filters)
        this.treeData = data
        this.rowTableData = []
        this.getTableData(this.treeData)
        this.total = data.length || 0
        // this.AddTaleData(this.treeData, this.total)
        this.tableLoading = false
      } catch {
        this.tableLoading = false
      }
    },
    // 空白行添加
    AddTaleData(arr, num) {
      const tableHeight = this.$refs.table.offsetHeight
      const row = Math.floor(
        (tableHeight - 36) / 36 // 表格：表头/行高：36px
      )
      for (let i = 0; i < (row - num); i++) {
        this.treeData.push({})
      }
    },
    /* 表格选中行事件 start */
    // 单击每行时每行的复选框选中
    rowChecked(row) {
      const selectData = this.multipleSelection
      this.$refs.multipleTable.clearSelection()
      if (selectData.length === 1) {
        selectData.forEach(item => {	// 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 选择每行表格前的复选框
    selectCheck(selection) {
      if (selection[0] !== undefined) {
        if (JSON.stringify(selection[0]) === '{}') {
          selection = []
          this.$refs.multipleTable.clearSelection()
        }
      }
      this.multipleSelection = selection

      this.single = selection.length === 0

      this.multipleSelection = selection
      if (selection.length > 1) {
        this.$refs.multipleTable.clearSelection()
        this.$refs.multipleTable.toggleRowSelection(selection.pop())
      }
      this.selectRow = []
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item, index) => {
          this.selectRow.push(this.rowTableData.indexOf(item))
        })
      }
    },
    // 高亮
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    // 选择表格前面的对话框时触发的方法
    getTableData(obj) {
      for (const i in obj) {
        this.rowTableData.push(obj[i])
        if (obj[i].children) {
          this.getTableData(obj[i].children)
        }
      }
    },
    /* 表格选中行事件 end */

    /* 按钮 start */
    // 搜索方法
    handleQuery() {
      this.getList()
    },
    // 重置
    resetQuery() {
      this.resetForm('filters')
      this.getList()
    },
    // 点击添加菜单弹出的对话框
    handleAdd() {
      const multip = this.multipleSelection
      this.$refs.addDialog.handleAdd('新增企业经营范围', undefined, multip.length && multip[0])
    },
    // 编辑
    handleUpdate() {
      this.$refs.addDialog.handleAdd('修改企业经营范围', this.multipleSelection[0].id)
    },
    // 详情
    handleDetail() {
      this.$refs.detailDialog.handleAdd('企业经营范围详情', this.multipleSelection[0].id)
    },
    // 删除操作
    handleDelete() {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await removeBusinessScope(this.multipleSelection[0].id)
          this.getList()
          this.msgSuccess('删除成功!')
        })
        .catch(() => {})
    }
  }
}
</script>

<style scoped>
</style>
